package com.core.microBlogs.sina.api.oauth
{
	import com.core.microBlogs.standard.Oauth;
	import com.util.OauthUrlUtil;
	import com.util.http.HttpConnection;
	import com.util.http.HttpEvent;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import json.JSON;
	
	public class DoOauth
	{
		public const REQUEST_TOKEN_URL:String = "http://api.t.sina.com.cn/oauth/request_token";
		public const OAUTH_TOKEN_URL:String = "http://api.t.sina.com.cn/oauth/authorize";
		public const ACCESS_TOKEN_URL:String = "http://api.t.sina.com.cn/oauth/access_token";
		
		public static const CMD_REQUEST_TOKEN:String = "CMD_REQUEST_TOKEN";
		public static const CMD_OAUTH_TOKEN:String = "CMD_ACCESS_TOKEN";
		public static const CMD_ACCESS_TOKEN:String = "CMD_ACCESS_TOKEN";
		
		private var _httpConn:HttpConnection;
		private var _dataHandler:Function;
		private var _errorHandler:Function;
		
		public function DoOauth(dataHandler:Function, errorHandler:Function)
		{
			this._dataHandler = dataHandler;
			this._errorHandler = errorHandler;
			_httpConn = new HttpConnection();
			initEvents();
		}
		
		//获取request_token
		public function getRequestToken():void{
			var key:OauthKeySina = new OauthKeySina();
			Oauth.oauthingKey = key;
			Oauth.oauthingKey.httpMethod = "GET";
			var paras:Array = [];
			var url:String = OauthUrlUtil.getOauthUrl(REQUEST_TOKEN_URL, key.httpMethod, key.customKey, 
				key.customSecrect, key.tokenKey, key.tokenSecrect, 
				key.verify, key.callbackUrl, paras);
			
			_httpConn.sendRequest(requestTokenHandler, url);
			
			function requestTokenHandler(data:String):void{
				var params:Object = executeResponse(CMD_REQUEST_TOKEN, data);
				onHttpDataHandler(CMD_REQUEST_TOKEN, params);
			}
		}
		
		//用户授权request_token
		public function authRequestToken():void{
			var url:String = OAUTH_TOKEN_URL + "?" + "oauth_token=" + Oauth.oauthingKey.tokenKey;
			var urlRequest:URLRequest = new URLRequest(url);
			navigateToURL(urlRequest);
		}
		
		//交换access_token
		public function getAccessToken():void{
			var paras:Array = [];
			Oauth.oauthingKey.httpMethod = "GET";
			var url:String = OauthUrlUtil.getOauthUrl(ACCESS_TOKEN_URL, 
				Oauth.oauthingKey.httpMethod, Oauth.oauthingKey.customKey, 
				Oauth.oauthingKey.customSecrect, Oauth.oauthingKey.tokenKey, 
				Oauth.oauthingKey.tokenSecrect, Oauth.oauthingKey.verify, 
				Oauth.oauthingKey.callbackUrl, paras);
			
			_httpConn.sendRequest(accessTokenHandler, url);
			
			function accessTokenHandler(data:String):void{
				var params:Object = executeResponse(CMD_ACCESS_TOKEN, data);
				onHttpDataHandler(CMD_ACCESS_TOKEN, params);
			}
		}
		
		private function onHttpDataHandler(cmd:String, params:Object):void{
			switch(cmd)
			{
				case DoOauth.CMD_REQUEST_TOKEN:
				{
					Oauth.oauthingKey.tokenKey = params.oauth_token;
					Oauth.oauthingKey.tokenSecrect = params.oauth_token_secret;
					break;
				}
				case DoOauth.CMD_ACCESS_TOKEN:
				{
					Oauth.currentAccountKey = Oauth.oauthingKey;
					Oauth.currentAccountKey.accountName = params.user_id;
					Oauth.currentAccountKey.tokenKey = params.oauth_token;
					Oauth.currentAccountKey.tokenSecrect = params.oauth_token_secret;
					break;
				}
				default:
				{
					break;
				}
			}
			_dataHandler(cmd, params);
		}
		
		private function onHttpErrorHandler(evt:HttpEvent):void{
			_errorHandler(evt.params);
		}
		
		private function initEvents():void{
			_httpConn.addEventListener(HttpEvent.onHttpError, onHttpErrorHandler);
		}
		
		private function executeResponse(cmd:String, data:String):Object
		{
			var params:Object = {};
			var datas:Array = data.split("&");
			for(var i:int = 0; i < datas.length; i++){
				var key:String = datas[i].split("=")[0];
				var value:String = datas[i].split("=")[1];
				params[key] = value;
			}
			return params;
		}
	}
}